package centerservice

import (
	"admin100/app/dao/centerdao"
	"admin100/app/model/centermodel"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"admin100/library/utils"
	"fmt"

	"github.com/gogf/gf/frame/g"
)

var CGameNode = cGameNodeService{}

type cGameNodeService struct{}

// 通过平台id和区服id获取*centermodel.CServerNode
// func (s *cGameNodeService) FetchServerNodeByPtIdAndSid(ptId, sid string) *centermodel.CServerNode {
// 	sNode, err := centerdao.CServerNode.As("S1").Fields("DISTINCT S1.*").
// 		InnerJoin(centerdao.CGameServer.Table, "S2",
// 			"S2."+centerdao.CGameServer.Columns.Node+"=S1."+centerdao.CServerNode.Columns.Node).
// 		Where(g.Map{
// 			"S2." + centerdao.CGameServer.Columns.PlatformId: ptId,
// 			"s2." + centerdao.CGameServer.Columns.Sid:        sid,
// 		}).FindOne()
// 	if err != nil {
// 		defErr.Panic(define.DataOptFail, err.Error())
// 	}
// 	// findOne查询不到不会报 sql.ErrNoRows
// 	if sNode == nil {
// 		defErr.Panic(define.RecordNodFound)
// 	}

// 	return sNode
// }
func (s *cGameNodeService) GetGameURLByNode(sNode string) string {
	if sNode == "" {
		return ""
	}
	sn, err := centerdao.CServerNode.Where(centerdao.CServerNode.Columns.Node, sNode).FindOne()
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}
	if sNode == "" {
		return ""
	}

	url := fmt.Sprintf("http://%s:%d", sn.Ip, sn.WebPort)
	return url

}

func (s *cGameNodeService) GetGameURLSliceByNode(nodes []string, path string) []string {
	var (
		res []string
	)
	for path[0] == '/' {
		path = path[1:]
	}

	data, err := centerdao.CServerNode.Where(centerdao.CServerNode.Columns.Node, nodes).FindAll()
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}

	for _, v := range data {
		url := fmt.Sprintf("http://%s:%d/"+path, v.Ip, v.WebPort)
		res = append(res, url)
	}

	return utils.RemoveRedata(res)
}

//
func (s *cGameNodeService) GetServerNode(node string) *centermodel.CServerNode {
	r, err := centerdao.CServerNode.Where(
		centerdao.CServerNode.Columns.Node, node).FindOne()
	defErr.IfPanic(err, define.RecordNodFound)
	if r == nil {
		defErr.Panic(define.RecordNodFound)
	}
	return r
}

// 获取所有游戏节点数据
func (s *cGameNodeService) getAllGameServerNodeByPlatformId(platformId string) []*centermodel.CServerNode {

	data, err := centerdao.CServerNode.Where(g.Map{
		centerdao.CServerNode.Columns.Type:       1,
		centerdao.CServerNode.Columns.PlatformId: platformId,
	}).FindAll()
	defErr.SelectRetPanic(err, data, define.DataOptFail)

	return data
}

// 获取所有游戏节点
func (s *cGameNodeService) GetAllGameNodeByPlatformId(platformId string) []string {
	data := make([]string, 0)
	serverNodeList := s.getAllGameServerNodeByPlatformId(platformId)
	for _, e := range serverNodeList {
		data = append(data, e.Node)
	}
	return data
}

func (s *cGameNodeService) FetchNodeStrByPtAndType(ptId string, typeId int) []*centermodel.CServerNode {
	if ptId == "" || typeId < 0 {
		return nil
	}

	res, err := centerdao.CServerNode.Fields(centerdao.CServerNode.Columns.Node).Where(g.Map{
		centerdao.CServerNode.Columns.PlatformId: ptId,
		centerdao.CServerNode.Columns.Type:       typeId,
	}).FindAll()
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}

	return res
}
